#!/bin/bash
#
# Script to convert 3DoF+ test material according to the CfTM format to the format that will be used for the CTC.
#
# Author: Bart Kroon
#         bart.kroon@philips.com

trap exit ERR
set -x

function unpack_sequence
{
    trap exit ERR
    
    mkdir -p "source/$1"
    cd "source/$1"
    
    case $1 in
        ClassroomVideo)
            for archive in ../../input/$1/$1-v*.tar.gz
            do
                tar xaf "$archive"
            done
            rm *.depth
            tar xaf "../../input/$1/$1_altdepth-v0_v14-4096x2048-0001_0120.tar.gz"
            ln -s ../../input/$1/$1.json metadata.json
            ;;
        *)
            for archive in ../../input/$1/*.zip
            do
                unzip -j "$archive"
            done
            ln -s ../../input/$1/metadata.json .
            ;;
    esac
    
    touch OK
	cd ../..
}

function do_convert_sequence
{
    trap exit ERR
    mkdir -p "output/$1"
    cd "output/$1"
    
    case $1 in
        ClassroomVideo)
            views=(v0 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14)
            nframes=120
            texture_fmt="%s_4096_2048_420_8b_%04d.yuv"
            depth_fmt="%s_4096_2048_0_8_1000_0_%04d.depth"
            output_texture_stream_fmt="v%d_4096_2048_420_10b.yuv"
            output_depth_stream_fmt="v%d_4096_2048_0_8_1000_0_420_10b.yuv"
            width=4096
            height=2048
            ;;
        TechnicolorHijack)
            views=(camEA01 camEA02 camEA03 camEA04 camEA05 camEA11 camEA12 camEA31 camEA32 camEA00)
            nframes=300
            texture_fmt="%s_4096_4096_420_8b_%04d.yuv"
            depth_fmt="%s_4096_4096_0_5_25_0_%04d.depth"
            output_texture_stream_fmt="v%d_4096_4096_420_10b.yuv"
            output_depth_stream_fmt="v%d_4096_4096_0_5_25_0_420_10b.yuv"
            width=4096
            height=4096
            ;;
        TechnicolorMuseum)
            views=(camE0 camE1 camE2 camE3 camE4 camE5 camE6 camE7 camE8 camE9 camE10 camE11 camE12 camE13 camE14 camE15 camE16 camE17 camE18 camE19 camE20 camE21 camE22 camE23)
            nframes=300
            texture_fmt="%s_2048_2048_420_8b_%04d.yuv"
            depth_fmt="%s_2048_2048_0_5_25_0_%04d.depth"
            output_texture_stream_fmt="v%d_2048_2048_420_10b.yuv"
            output_depth_stream_fmt="v%d_2048_2048_0_5_25_0_420_10b.yuv"
            width=2048
            height=2048          
            ;;
    esac
    
    for index in ${!views[*]}
    do
        view=${views[$index]}
        output_texture_stream=`printf $output_texture_stream_fmt $index`
        output_depth_stream=`printf $output_depth_stream_fmt $index`

        for ((frame=1; frame<=$nframes; frame++))
        do
            HDRConvert -f ../../HDRConvert_texture.cfg \
                -p SourceFile=../../source/$1/`printf $texture_fmt $view $frame` \
                -p SourceWidth=$width -p SourceHeight=$height \
                -p OutputWidth=$width -p OutputHeight=$height \
                -p SilentMode=1
                
            cat output_conv.yuv >> $output_texture_stream
            
            HDRConvert -f ../../HDRConvert_depth.cfg \
                -p SourceFile=../../source/$1/`printf $depth_fmt $view $frame` \
                -p SourceWidth=$width -p SourceHeight=$height \
                -p OutputWidth=$width -p OutputHeight=$height \
                -p SilentMode=1
            
            cat output_conv.yuv >> $output_depth_stream
        done
    done
    
    touch OK
	cd ../..
}

function convert_sequence
{
    trap exit ERR
    
    if [ ! -f "output/$1/OK" ]
    then
        if [ ! -f "source/$1/OK" ]
        then
            unpack_sequence $1
        fi
    
        do_convert_sequence $1
    fi
}

convert_sequence ClassroomVideo
convert_sequence TechnicolorMuseum
convert_sequence TechnicolorHijack # Issue with a zipfile

